package view;

import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import model.State;

/**
 * Class to create a choice panel.
 * @author Sarah George
 * @version Autumn 2012
 */
@SuppressWarnings("serial")
public class ChoicePanel extends JPanel implements Observer{
	
	/**
	 * Field to hold the current state of the system.
	 */
	private State my_state;
	
	/**
	 * Field to hold the JFrame in which this panel will live.
	 */
	private JFrame my_frame;
	
	/**
	 * Field to hold a JButton for login.
	 */
	private JButton login;

	/**
	 * Constructor for a choice panel
	 * @param the_state the current state of the system
	 * @param the_frame the JFrame in which the panel lives.
	 */
	public ChoicePanel(State the_state, JFrame the_frame) {
		super();
		my_state = the_state;
		my_state.addObserver(this);
		my_frame = the_frame;
		setup();
	}
	
	/**
	 * Method to setup the choice panel.
	 */
	public void setup() {
		CloseAction ca = new CloseAction(my_frame);
		JButton cancel = new JButton(ca);
		cancel.setText("Cancel");
		LoginAction la = new LoginAction(my_state, my_frame);
		login = new JButton(la);
		login.setVisible(false);
		this.add(cancel);
		this.add(login);
	}
	
	/**
	 * Method to allow login to occur.
	 */
	public void allowLogin()
	{
		login.setVisible(true);
		repaint();
	}

	/**
	 * Method to update the frame when the observable is ready.
	 * @param o the observable
	 * @param arg the object that called notify observers.
	 */
	@Override
	public void update(Observable o, Object arg) {

		if (arg instanceof Integer)
			login.setVisible(((Integer) arg == State.ALLOW_LOGIN));	
	}
}//End class choice panel